package atguigu.str;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;  
import java.nio.charset.StandardCharsets;  
  
public class MD5Encryption {  
  
    public static String getMD5Hash(String input) {  
        try {  
            // 获取MD5 MessageDigest实例  
            MessageDigest md = MessageDigest.getInstance("MD5");  
            // 更新要加密的数据  
            md.update(input.getBytes(StandardCharsets.UTF_8));  
            // 完成加密计算  
            byte[] digest = md.digest();  
            // 将加密后的字节转换为十六进制字符串  
            StringBuilder sb = new StringBuilder();  
            for (byte b : digest) {  
                sb.append(String.format("%02x", b & 0xff));  
            }  
            return sb.toString();  
        } catch (NoSuchAlgorithmException e) {  
            throw new RuntimeException("MD5 not supported", e);  
        }  
    }  
  
    public static void main(String[] args) {  
        String input = "Hello, World!";  
        String md5Hash = getMD5Hash(input);  
        System.out.println("MD5 Hash of \"" + input + "\": " + md5Hash);  
    }  
}